﻿using UnityEngine;
using System.Collections;

/// <summary>
/// 玩家飞机飞行
/// </summary>
public class PlayerMove : MonoBehaviour
{
    public float flySpeed = 20f;
    float maxX;
    float maxY;

    void Start()
    {
        Vector3 screenMax = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height));
        maxX = screenMax.x;
        maxY = screenMax.y;
    }

    void Update()
    {
        if (Input.GetMouseButton(0))
        {
            Vector3 pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            pos.z = 0;

            pos.x = Mathf.Clamp(pos.x, -maxX, maxX);
            pos.y = Mathf.Clamp(pos.y, -maxY, maxY);

            Vector3 vec = pos - transform.position;
            if (Vector3.SqrMagnitude(vec) < 0.5f)
            {
                transform.position = pos;
            }
            else
            {
                Vector2 dir = Vector3.Normalize(vec);
                transform.Translate(dir * flySpeed * Time.deltaTime);
            }
        }
    }
}
